<?php
/* 
* @Title:  [商品详情页控制器]
* @Author: {chenlei} [chenleib5@126.com]
* @Date:   2014-04-09 08:40:42
* @Last Modified by:   Administrator
* @Last Modified time: 2014-04-27 23:11:38
* @Copyright:  [hn7m.com]
*/
class DetailsControl extends CommonControl{
	private $goodsView=NULL;
	private $combineModel=NULL;
	private $categoryModel=NULL;
	private $commentModel=NULL;
	/**
	 * [__init 自动加载模型]
	 * @return [type] [description]
	 */
	public function __init(){
		parent::__init();
		$this->goodsView=K('GoodsView');  //加载商品视图模型
		$this->combineModel=K('Combine'); //加载货品表模型
		$this->categoryModel=K('Category'); //加载分类模型
		$this->commentModel=K('Comment');//加载评价模型
	}
	/**
	 * [index 默认显示商品详情]
	 * @return [type] [description]
	 */
	public function index(){
		$gid=Q('get.gid',0,'intval');

		$details=$this->goodsView->join(array('goods_details'))->get_one_data($gid);
		$this->assign('details',$details);  //分配商品信息
		
		$size=$this->combineModel->get_combineSize($gid);
		$color=$this->combineModel->get_combineColor($gid);
		$g=$this->commentModel->get_num(array('qm_goods_gid'=>$gid,'mgorb'=>'2'));
		$all=$this->commentModel->get_num(array('qm_goods_gid'=>$gid));
		$all=($all==0)?1:$all; //避免分母为0;
		$radio=$g/$all;
		$radio=($radio==0)?1:$radio;
		$radio = (sprintf("%.2f",$radio))*100;
		$this->assign('size',$size); //分配尺码
		$this->assign('color',$color); //分配颜色
		$this->assign('radio',$radio);//分配好评率
		$this->assign('comnum',$all);//分配评价数

		$cid=$details['qm_category_cid'];
		$type=$this->categoryModel->get_onedate_value($cid,'cname');
		$this->assign('type',$type); //分配当前分类

		$comment=$this->commentModel->get_data(array('qm_goods_gid'=>$gid),'mtime DESC');
		if(!empty($comment)){
			foreach ($comment as $key => $value) {
				$comment[$key]['uaccount']=K('User')->get_onedate_value($value['qm_user_uid'],'uaccount');
				$comment[$key]['uname']=K('User')->get_onedate_value($value['qm_user_uid'],'uname');
			}
		}
		$this->assign('comment',$comment);//分配评论

		$this->display();
	}
	/**
	 * [ajax_get_inventory 异步获得组合尺码和颜色的商品的库存量]
	 * @return [type] [description]
	 */
	public function ajax_get_inventory(){
		$gid=Q('get.gid',0,'intval');
		$bcontent=Q('post.combine','','htmlspecialchars');
		$binventory=$this->combineModel->get_one_value($gid,$bcontent);
		echo json_encode($binventory);die();
	}
	/**
	 * [ajax_add_cart 异步加入购物车]
	 * @return [type] [description]
	 */
	public function ajax_add_cart(){
		$gid=Q('get.gid',0,'intval');
		$postData=Q('post.');
		$data = array(
			'id' 	=> $gid, // 商品 ID
			'name'	=>$postData['name'],// 商品名称
			'num' 	=>$postData['num'], // 商品数量
			'price' =>$postData['price'], // 商品价格
			'place'	=>$postData['place'], // 商品产地
			'cate'	=>$postData['cate'], // 商品分类
			'imgUrl'=>$postData['imgUrl'], // 商品分类
			'kucun'	=>$postData['kucunNum'],//商品库存
			'bid'	=>$postData['bid'],//货品表ID
			'options' => array(// 其他参数如价格、颜色、可以为数组或字符串
				'size' =>$postData['size'],
				'color' =>$postData['color']
			)
		);
		cart::add($data); // 添加到购物车;
		//如果是登陆状态,改变session的同时改变数据库cart表
        if(isset($_SESSION['uid'])&&isset($_SESSION['username'])){
            $uid=$_SESSION['uid'];
            $this->session_to_cart($uid);  //此方法写在通用控制器中;
        }
		$this->ajax($_SESSION);
	}
	/**
	 * [commnet 发布评价]
	 * @return [type] [description]
	 */
	public function comment(){
		if(IS_POST){
			$gid=Q('post.gid',0,'intval');
			$data=array(
				'mcontent'	=>Q('post.mcontent','','htmlspecialchars'),
				'mgorb'		=>Q('post.mgorb'),
				'mtime'		=>time(),
				'qm_goods_gid'	=>$gid,
				'qm_user_uid'	=>Q('session.uid',0,'intval')
				);
			$this->commentModel->add_data($data);
			go(U('Details/index',array('gid'=>$gid)));
		}
	}
}
?>